package com.projectx.action.contact;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.projectx.R;
import com.projectx.action.chat.SwitchChat;

import java.util.List;

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.VH> {

    private Context context;
    private List<String> data;
    private  List<Bitmap>bitmaps;
    private boolean isGroup;
    public RVAdapter(Context context, List<String> data, List<Bitmap>bitmaps,boolean isGroup) {
        this.isGroup=isGroup;
        this.context = context;
        this.data = data;
        this.bitmaps=bitmaps;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);
        return new VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final VH holder, int position) {
        holder.textView.setText(data.get(position));
        try {
            holder.imageView.setImageBitmap(bitmaps.get(position));
            if(bitmaps.get(position).equals(null)){
                holder.imageView.setImageResource(R.drawable.ic_account_circle_black_36dp);
            }
        }catch (NullPointerException e){
            holder.imageView.setImageResource(R.drawable.ic_account_circle_black_36dp);
        }catch (IndexOutOfBoundsException e){
            Log.e("lzw","头像list越界了");
        }
        holder.click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // 点击事件
               SwitchChat.startChat(context,"测试",holder.textView.getText().toString(),isGroup);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class VH extends RecyclerView.ViewHolder {

        TextView textView;
        ImageView imageView;
        View click;
        public VH(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
            imageView= itemView.findViewById(R.id.imageView);
            click=itemView.findViewById(R.id.click);
        }
    }
}
